package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.dic.SexEnum;
import com.ruoyi.system.domain.TDrugInfo;
import com.ruoyi.system.domain.TPrescriptionHistoryInfo;
import com.ruoyi.system.domain.TPrescriptionRecords;
import com.ruoyi.system.service.ITDrugInfoService;
import com.ruoyi.system.service.ITPrescriptionHistoryInfoService;
import com.ruoyi.system.service.ITPrescriptionRecordsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/nurse")
public class TNurseController extends BaseController {
    private String prefix = "system/nurse";

    @Autowired
    private ITPrescriptionHistoryInfoService tPrescriptionHistoryInfoService;

    @Autowired
    private ITPrescriptionRecordsService tPrescriptionRecordsService;

    @Autowired
    private ITDrugInfoService tDrugInfoService;

    @RequiresPermissions("system:nurse:view")
    @GetMapping()
    public String nurse() {
        return prefix + "/nurse";
    }


    /**
     * 查询处方的药品信息列表
     */
    @RequiresPermissions("system:nurse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPrescriptionHistoryInfo tPrescriptionHistoryInfo) {
        startPage();
        List<TPrescriptionHistoryInfo> list = tPrescriptionHistoryInfoService.selectOtherHistoryInfoList(tPrescriptionHistoryInfo);
        list = list.stream().filter(e -> e.getStatus() == 2).collect(Collectors.toList());
        setDaysDetail(list);

        list.sort(new Comparator<TPrescriptionHistoryInfo>() {
            @Override
            public int compare(TPrescriptionHistoryInfo person1, TPrescriptionHistoryInfo person2) {
                return (int) (person1.getAdd_time().getTime() - person2.getAdd_time().getTime());
            }
        });

        return getDataTable(list);
    }

    private void setDaysDetail(List<TPrescriptionHistoryInfo> datas) {
        datas.stream().peek(e -> {
            int less = e.getDaysDetail() - e.getOver_days();
            e.setDaysDetailDesc(e.getDaysDetail() + "天余" + less);
            e.setCustomerSex(SexEnum.getByCode(e.getCustomerSex()));
        }).collect(Collectors.toList());
    }


    /**
     * 展示处方信息
     */
    @RequiresPermissions("system:nurse:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        TPrescriptionRecords tPrescriptionRecords = tPrescriptionRecordsService.selectTPrescriptionRecordsById(id);
        mmap.put("tPrescriptionRecords", tPrescriptionRecords);
        mmap.put("nurseid", getSysUser().getUserId());
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
        String dt1 = dateFormat.format(tPrescriptionRecords.getCrtime()); // 处方日期
        String dt2 = dateFormat.format(new Date()); // 当前日期
        boolean isSameDay = dt1.equals(dt2); // 处方日期是不是今天
        // 当天并且有皮试才能返回医生
        mmap.put("canback", tPrescriptionRecords.getHasps() == 1);
        return prefix + "/detail";
    }


//    /**
//     * 修改-处方信息
//     */
//    @RequiresPermissions("system:nurse:edit")
//    @Log(title = "处方信息", businessType = BusinessType.UPDATE)
//    @PostMapping("/updatePrescription")
//    @ResponseBody
//    public AjaxResult updatePrescription(TPrescriptionRecords tPrescriptionRecords) {
//        return toAjax(tPrescriptionRecordsService.updateTPrescriptionRecords1(tPrescriptionRecords));
//    }

}
